/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bff.core.action.modify;

import com.inspur.edp.bef.api.action.VoidActionResult;
import com.inspur.edp.bff.api.dataprocessor.IChangeConvertor;
import com.inspur.edp.bff.api.manager.context.ChangeMapperContext;
import com.inspur.edp.bff.api.manager.context.ModifyContext;
import com.inspur.edp.bff.spi.AbstractFSAction;
import com.inspur.edp.cef.entity.changeset.IChangeDetail;

public class ModifyAction extends AbstractFSAction<VoidActionResult> {


    private IChangeDetail change;

    public ModifyAction(IChangeDetail change) {
        this.change = change;
    }

    @Override
    protected void execute() {
        if (getContext().getAssemblerManager() == null) {
            doDefaultModify();
            return;
        }
        ModifyContext modifyContext = new ModifyContext();
        modifyContext.getSourceChange().add(change);
        //① BeforeModify
        getContext().getAssemblerManager().getModifyAssembler().doBeforeModify(getContext(), modifyContext);

        //② ChangeMapping
        ChangeMapperContext mapperContext = new ChangeMapperContext();
        mapperContext.setSourceChange(modifyContext.getSourceChange());
        getContext().getAssemblerManager().getChangeMapperAssembler().doChangeMappingFromVo(getContext(), mapperContext);

        //③ DoModify
        //modifyContext.SourceChange = mapperContext.SourceChange;
        modifyContext.setMappedChange(mapperContext.getMappedChange());
        getContext().getAssemblerManager().getModifyAssembler().doModify(getContext(), modifyContext);

        //④ AfterModify
        getContext().getAssemblerManager().getModifyAssembler().doAfterModify(getContext(), modifyContext);


    }

    private void doDefaultModify() {
        IChangeConvertor changeConvertor = getContext().getChangeConvertor();
        IChangeDetail beChange = changeConvertor.convertFromVMChange(change);
        getLcp().modify(beChange);
    }
}
